from State import State
from os.path import isfile
from timer import keep_time, power_set
from config import get_config
from ignition_check import check_position


if isfile("./config.py"):
    from config import get_config

others_datas = get_config()["others"]
pv_datas = get_config()["PV"]


# class Mode_Init(State):
#     def handle(self, fsm):
#         if (
#             not others_datas["开环功率范围"][0]
#             < self.pf.create_pv(pv_datas["当前功率"]).get()
#             < others_datas["开环功率范围"][1]
#         ):
#             print("设定开环功率为[{}-{}]W".format(*others_datas["开环功率范围"]))
#             self.pf.create_pv(pv_datas["幅度设定点"]).put(
#                 self.pf.create_pv(pv_datas["开环维持功率设置"]).get()
#             )
#             return self
#         else:
#             # 跳转到某状态
#             return fsm.mode_openloop


class Mode_OpenLoop(State):
    def handle(self, fsm):
        if self.pf.create_pv(pv_datas["急停开关"]).get() == 1:
            print("急停触发")
            self.pf.create_pv(pv_datas["启动开关"]).put(0)
            self.pf.create_pv(pv_datas["幅度驱动"]).put(0)
            self.pf.create_pv(pv_datas["幅度设定点"]).put(
                self.pf.create_pv(pv_datas["开环维持功率设置"]).get()
            )
        if self.pf.create_pv(pv_datas["数据清零"]).get() == 1:
            self.pf.create_pv(pv_datas["数据清零"]).put(0)
            self.pf.create_pv(pv_datas["当前清洗时间"]).put(0)
            self.pf.create_pv(pv_datas["清洗上部时间"]).put(0)
            self.pf.create_pv(pv_datas["清洗下部时间"]).put(0)
            self.pf.create_pv(pv_datas["清洗上部次数"]).put(0)
            self.pf.create_pv(pv_datas["清洗下部次数"]).put(0)
        if (
            not others_datas["开环功率范围"][0]
            < self.pf.create_pv(pv_datas["当前功率"]).get()
            < others_datas["开环功率范围"][1]
        ):
            return fsm.mode_init
        else:
            print("完成开环，等待跳转闭环触发")
            if self.pf.create_pv(pv_datas["启动开关"]).get() == 1:
                print("设定闭环功率为[{}-{}]W".format(*others_datas["闭环功率范围"]))
                self.pf.create_pv(pv_datas["幅度驱动"]).put(
                    self.pf.create_pv(pv_datas["闭环维持功率设置"]).get()
                )
                return fsm.mode_closeloop
            else:
                return self


class Mode_CloseLoop(State):
    def handle(self, fsm):

        if self.pf.create_pv(pv_datas["急停开关"]).get() == 1:
            return fsm.mode_openloop
        elif not (
            others_datas["闭环功率范围"][0]
            < self.pf.create_pv(pv_datas["当前功率"]).get()
            < others_datas["闭环功率范围"][1]
        ):
            print("设定闭环功率为[{}-{}]W".format(*others_datas["闭环功率范围"]))
            self.pf.create_pv(pv_datas["幅度驱动"]).put(
                self.pf.create_pv(pv_datas["闭环维持功率设置"]).get()
            )
            return self
        else:
            print("进入闭环,等待触发")
            if self.pf.create_pv(pv_datas["闭环至升功率开关"]).get() == 1:
                return fsm.mode_increasepower
            else:
                return self


class Mode_IncreasePower(State):

    def handle(self, fsm):
        if self.pf.create_pv(pv_datas["急停开关"]).get() == 1:
            return fsm.mode_openloop
        elif (
            self.pf.create_pv(pv_datas["当前功率"]).get()
            <= self.pf.create_pv(pv_datas["点火功率设置"]).get()
        ):
            if (
                abs(
                    self.pf.create_pv(pv_datas["当前功率"]).get()
                    - self.pf.create_pv(pv_datas["点火功率设置"]).get()
                )
                <= others_datas["功率误差"]
            ):
                print("升功率到达点火功率附近")
                keep_time(self.pf.create_pv(pv_datas["点火维持时间"]).get())
                return fsm.mode_clean
            else:
                power_set(
                    self.pf.create_pv(pv_datas["当前功率"]).get(),
                    self.pf.create_pv(pv_datas["点火功率设置"]).get(),
                    self.pf.create_pv(pv_datas["点火时间"]).get(),
                )
                return self
        elif (
            self.pf.create_pv(pv_datas["当前功率"]).get()
            > self.pf.create_pv(pv_datas["点火功率设置"]).get()
        ):
            return fsm.mode_decreasepower


class Mode_DecreasePower(State):

    def handle(self, fsm):
        if self.pf.create_pv(pv_datas["急停开关"]).get() == 1:
            return fsm.mode_openloop
        elif (
            self.pf.create_pv(pv_datas["当前功率"]).get()
            >= self.pf.create_pv(pv_datas["点火功率设置"]).get()
        ):
            if (
                abs(
                    self.pf.create_pv(pv_datas["当前功率"]).get()
                    - self.pf.create_pv(pv_datas["点火功率设置"]).get()
                )
                <= others_datas["功率误差"]
            ):
                print("降功率到达点火功率附近")
                keep_time(self.pf.create_pv(pv_datas["点火维持时间"]).get())
                return fsm.mode_clean
            else:
                power_set(
                    self.pf.create_pv(pv_datas["当前功率"]).get(),
                    self.pf.create_pv(pv_datas["点火功率设置"]).get(),
                    self.pf.create_pv(pv_datas["点火时间"]).get(),
                )
                return self
        elif (
            self.pf.create_pv(pv_datas["当前功率"]).get()
            < self.pf.create_pv(pv_datas["点火功率设置"]).get()
        ):
            return fsm.mode_increasepower


class Mode_Clean(State):
    def handle(self, fsm):
        if self.pf.create_pv(pv_datas["急停开关"]).get() == 1:
            return fsm.mode_openloop
        else:
            power_set(
                self.pf.create_pv(pv_datas["当前功率"]).get(),
                self.pf.create_pv(pv_datas["清洗功率设置"]).get(),
                self.pf.create_pv(pv_datas["清洗时间"]).get(),
            )
            if (
                abs(
                    self.pf.create_pv(pv_datas["当前功率"]).get()
                    - self.pf.create_pv(pv_datas["清洗功率设置"]).get()
                )
                <= others_datas["功率误差"]
            ):
                check_position()
                keep_time(self.pf.create_pv(pv_datas["清洗时间"]).get(), flag=True)
            if self.pf.create_pv(pv_datas["降维持开关"]).get() == 1:
                power_set(
                    self.pf.create_pv(pv_datas["当前功率"]).get(),
                    self.pf.create_pv(pv_datas["降功率清洗功率"]).get(),
                    self.pf.create_pv(pv_datas["降功率时间"]).get(),
                )
                if (
                    abs(
                        self.pf.create_pv(pv_datas["当前功率"]).get()
                        - self.pf.create_pv(pv_datas["降功率清洗功率"]).get()
                    )
                    <= others_datas["功率误差"]
                ):
                    check_position()
                    keep_time(
                        self.pf.create_pv(pv_datas["降功率清洗时间"]).get(), flag=True
                    )
            if self.pf.create_pv(pv_datas["升维持开关"]).get() == 1:
                power_set(
                    self.pf.create_pv(pv_datas["当前功率"]).get(),
                    self.pf.create_pv(pv_datas["升功率清洗功率"]).get(),
                    self.pf.create_pv(pv_datas["升功率时间"]).get(),
                )
                if (
                    abs(
                        self.pf.create_pv(pv_datas["当前功率"]).get()
                        - self.pf.create_pv(pv_datas["升功率清洗功率"]).get()
                    )
                    <= others_datas["功率误差"]
                ):
                    check_position()
                    keep_time(
                        self.pf.create_pv(pv_datas["升功率清洗时间"]).get(), flag=True
                    )
            power_set(
                self.pf.create_pv(pv_datas["当前功率"]).get(),
                self.pf.create_pv(pv_datas["闭环维持功率设置"]).get(),
                self.pf.create_pv(pv_datas["清洗结束降功率时间"]).get(),
            )
            if (
                abs(
                    self.pf.create_pv(pv_datas["当前功率"]).get()
                    - self.pf.create_pv(pv_datas["闭环维持功率设置"]).get()
                )
                <= others_datas["功率误差"]
            ):
                keep_time(self.pf.create_pv(pv_datas["清洗结束闭环维持时间"]).get())
            return fsm.mode_closeloop
